﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Hosting;

namespace Migration.Classes
{
	public static class MigrationMasterPages
	{
		public static void ImportMasterPage(Telerik.Sitefinity.Services.Migration.MigrationMasterPage masterPageContent)
		{
			var rootPath = HostingEnvironment.MapPath("~/App_Master");

			if (!Directory.Exists(rootPath))
			{
				Directory.CreateDirectory(rootPath);
			}

			var masterPagePath = HostingEnvironment.MapPath(String.Format("~/App_Master/{0}", masterPageContent.MasterPageName));

			if (!File.Exists(masterPagePath))
			{
				string fileContent = masterPageContent.MasterPageContents;
				if (MigrationSettings.RewriteRegisterTags)
				{
					fileContent = MigrationControls.ParseMasterAspxAscxContent(masterPageContent.MasterPageContents);
				}

				File.WriteAllText(masterPagePath, fileContent);

				MigrationLogger.AppendLogMessage(String.Format("Imported master page \"<b>~/App_Master/{0}</b>\"", masterPageContent.MasterPageName));
			}
		}
	}
}
